package paint;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

public class DefaultPaintText extends PaintComponent {
	protected String text;
	protected String[] highlights;

	protected Color textColor = Color.WHITE;
	protected Color highlightColor;
	protected Color backgroundColor;
	protected Color outlineColor;

	protected boolean highlightText;
	protected boolean highlightDigits;
	protected boolean underline;
	protected boolean striped = true;
	protected PaintContainer container = null;

	public DefaultPaintText(int x, int y, String text, String[] highlights, Color textColor, Color highlightColor,
			Color backgroundColor, Color outlineColor, boolean highlightDigits, boolean underline, boolean striped) {
		super(new Rectangle(x, y, 0, 0));

		this.text = text;
		this.highlights = highlights;
		this.textColor = textColor;
		this.highlightColor = highlightColor;
		this.backgroundColor = backgroundColor;
		this.outlineColor = outlineColor;
		highlightText = highlights != null;
		this.highlightDigits = highlightDigits;
		this.underline = underline;
		this.striped = striped;
	}

	public DefaultPaintText(int x, int y, String text, String[] highlights, Color textColor, Color highlightColor,
			Color backgroundColor, Color outlineColor, boolean highlightDigits, boolean underline, boolean striped,
			PaintContainer container) {
		super(new Rectangle(x, y, 0, 0));

		this.text = text;
		this.highlights = highlights;
		this.textColor = textColor;
		this.highlightColor = highlightColor;
		this.backgroundColor = backgroundColor;
		this.outlineColor = outlineColor;
		highlightText = highlights != null;
		this.highlightDigits = highlightDigits;
		this.underline = underline;
		this.striped = striped;
		this.container = container;
	}

	public DefaultPaintText(int x, int y, String text) {
		super(new Rectangle(x, y, 0, 0));
		this.text = text;
	}

	@Override
	public void paint(Graphics g) {
		FontMetrics fm = g.getFontMetrics();
		Rectangle textBounds = fm.getStringBounds(text, g).getBounds();
		bounds.width = textBounds.width;
		bounds.height = textBounds.height;

		if (container == null) {
			if (striped) {
				container = new StripedPaintContainer(outlineColor, backgroundColor, new Rectangle(getX() - 2, getY()
						- textBounds.height + 1, textBounds.width + 5, textBounds.height + 5));
			} else {
				container = new PaintContainer(outlineColor, backgroundColor, new Rectangle(getX() - 2, getY()
						- textBounds.height + 1, textBounds.width + 5, textBounds.height + 5));
			}
		}

		container.paint(g);

		for (int i = 1; i > -1; i--) {
			g.setColor(textColor);
			if (i == 1) {
				g.setColor(Color.BLACK);
			}

			g.drawString(text, getX() + 1, getY() + 1);
		}

		for (int i = 0; i < text.length(); i++) {
			if (Character.isDigit(text.charAt(i)) && highlightDigits) {
				g.setColor(highlightColor);
				g.drawString("" + text.charAt(i), getX() + 1 + fm.stringWidth(text.substring(0, i)), getY() + 1);
			}
		}

		if (highlightText) {
			int index = 0;
			String[] split = text.split(" ");
			for (String str : split) {
				for (String s : highlights) {
					if (str.toLowerCase().contains(s.toLowerCase())) {
						g.setColor(highlightColor);
						g.drawString(s, getX() + 1 + index + fm.stringWidth(str.replace(s, "")), getY() + 1);
					}
				}

				index += fm.stringWidth(str + " ");
			}
		}

		if (underline) {
			String underline = "";
			for (int i = 0; i < text.length(); i++) {
				underline += "_";
			}

			g.setColor(textColor);
			g.drawString(underline, getX() + 1, getY() + 2);
		}

		g.setColor(new Color(84, 84, 84, 80));
		Rectangle b = container.getBounds();
		g.fillRect(b.x, b.y, b.width, b.height / 2);
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub

	}

}
